package org.gy.T7;

import java.util.*;
import java.util.stream.Collectors;

import static sun.reflect.annotation.AnnotationParser.toArray;

public class StreamTest4 {
    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        Student s1 = new Student("蜘蛛精", 26, 172.5);
        Student s2 = new Student("蜘蛛精", 26, 172.5);
        Student s3 = new Student("紫霞", 23, 167.6);
        Student s4 = new Student("⽩晶晶", 25, 169.0);
        Student s5 = new Student("牛魔王",35,183.3);
        Student s6 = new Student("⽜夫⼈", 34, 168.5);
        Collections.addAll(students, s1, s2, s3, s4, s5, s6);

        // 需求1：请计算出身⾼超过168的学⽣有⼏⼈
        long size = students.stream().filter(s -> s.getHeight() > 168).count();
        System.out.println(size);

        // 需求2：请找出身⾼最⾼的学⽣对象，并输出。
        Student s = students.stream().max((o1, o2) ->
                Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(s);

        // 需求3：请找出身⾼最矮的学⽣对象，并输出。
        Student ss = students.stream().min((o1, o2) ->
                Double.compare(o1.getHeight(), o2.getHeight())).get();
        System.out.println(ss);

        // 需求4：请找出身⾼超过170的学⽣对象，并放到⼀个新集合中去返回。
// 流只能收集⼀次。
        List<Student> students1 = students.stream().filter(a -> a.getHeight() >
                170).collect(Collectors.toList());
        System.out.println(students1);
        Set<Student> students2 = students.stream().filter(a -> a.getHeight() >
                170).collect(Collectors.toSet());
        System.out.println(students2);

        // 需求5：请找出身⾼超过170的学⽣对象，并把学⽣对象的名字和身⾼，存⼊到⼀个Map集合返回。
        Map<String, Double> map =
                students.stream().filter(a -> a.getHeight() > 170)
                        .distinct().collect(Collectors.toMap(a -> a.getName(),
                                a -> a.getHeight()));
        System.out.println(map);
// Object[] arr = students.stream().filter(a -> a.getHeight() > 170).toArray();
        Student[] arr = students.stream().filter(a -> a.getHeight() >
                170).toArray(len -> new Student[len]);
        System.out.println(Arrays.toString(arr));
    }
}
